package com.ts.manage.cms.service;

import com.ts.framework.domain.cms.CmsPage;
import com.ts.framework.domain.cms.CmsSite;
import com.ts.framework.domain.cms.CmsTemplate;
import com.ts.framework.domain.cms.request.QueryPageRequest;
import com.ts.framework.domain.cms.response.CmsCode;
import com.ts.framework.domain.cms.response.CmsPageResult;
import com.ts.framework.exception.ExceptionCast;
import com.ts.framework.model.response.CommonCode;
import com.ts.framework.model.response.QueryResponseResult;
import com.ts.framework.model.response.QueryResult;
import com.ts.framework.model.response.ResponseResult;
import com.ts.manage.cms.dao.CmsPageRepository;
import com.ts.manage.cms.dao.CmsSiteRepository;
import com.ts.manage.cms.dao.CmsTemplateRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CmsPageService {
    @Autowired
    CmsPageRepository cmsPageRepository;
    @Autowired
    CmsSiteRepository cmsSiteRepository;
    @Autowired
    CmsTemplateRepository cmsTemplateRepository;


    //根据条件分页查询页面信息
    public QueryResponseResult findCmsPageByPage(Integer page, Integer size, QueryPageRequest queryPageRequest) {
        if (page <= 1) {
            page = 1;
        }
        page = page - 1;
        if (size < 1) {
            size = 10;
        }
        if (queryPageRequest == null) {
            queryPageRequest = new QueryPageRequest();
        }
        Pageable pageable = PageRequest.of(page, size);
        //创建值对象
        CmsPage cmsPage = new CmsPage();
        //非空校验
        if (!StringUtils.isEmpty(queryPageRequest.getSiteId())) {
            cmsPage.setSiteId(queryPageRequest.getSiteId());
        }
        if (!StringUtils.isEmpty(queryPageRequest.getTemplateId())) {
            cmsPage.setTemplateId(queryPageRequest.getTemplateId());
        }
        if (!StringUtils.isEmpty(queryPageRequest.getPageAliase())) {
            cmsPage.setPageAliase(queryPageRequest.getPageAliase());
        }
        if (!StringUtils.isEmpty(queryPageRequest.getPageName())) {
            cmsPage.setPageName(queryPageRequest.getPageName());
        }
        if (!StringUtils.isEmpty(queryPageRequest.getPageType())) {
            cmsPage.setPageType(queryPageRequest.getPageType());
        }
        //创建提供查询规则的对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("pageName", ExampleMatcher.GenericPropertyMatchers.contains());
//        解决包名映射问题
//                .withIgnoreNullValues()
//                .withIgnorePaths("_class");

        Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);
        Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);
        List<CmsPage> content = all.getContent();
        QueryResult<CmsPage> cmsPageQueryResult = new QueryResult<>();
        cmsPageQueryResult.setList(content);
        cmsPageQueryResult.setTotal(all.getTotalElements());
        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS, cmsPageQueryResult);
        return queryResponseResult;
    }

    //新增页面信息
    public CmsPageResult addCmsPage(CmsPage cmsPage) {
        //校验页面是否存在，根据页面名称、站点Id、页面webpath查询
        CmsPage cmsPageDb = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
        if (cmsPageDb == null) {
            //主键自动生成
            cmsPage.setPageId(null);
            cmsPageRepository.save(cmsPage);
            //返回结果
            CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS, cmsPage);
            return cmsPageResult;
        }
        return new CmsPageResult(CommonCode.FAIL, null);
    }

    //查询站点信息
    public QueryResponseResult querySiteId() {
        List<CmsSite> all = cmsSiteRepository.findAll();
        QueryResult<CmsSite> result = new QueryResult<>();
        result.setList(all);
        return new QueryResponseResult(CommonCode.SUCCESS, result);
    }

    //查询模板信息
    public QueryResponseResult queryTemplate() {
        List<CmsTemplate> all = cmsTemplateRepository.findAll();
        QueryResult<CmsTemplate> result = new QueryResult<>();
        result.setList(all);
        return new QueryResponseResult(CommonCode.SUCCESS, result);
    }

    //根据ID查询页面
    public CmsPageResult findCmsPageById(String id) {
        //参数校验
        if (StringUtils.isEmpty(id)) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Optional<CmsPage> optional = cmsPageRepository.findById(id);
        if (!optional.isPresent()) {
            ExceptionCast.cast(CmsCode.CMS_PAGE_NOTEXISTS);
        }
        CmsPage cmsPage = optional.get();
        return new CmsPageResult(CommonCode.SUCCESS, cmsPage);
    }

    //更新页面信息
    public CmsPageResult updateCmsPage(String id, CmsPage cmsPage) {
        if (StringUtils.isEmpty(id)) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        if (cmsPage == null) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Optional<CmsPage> optional = cmsPageRepository.findById(id);
        if (!optional.isPresent()) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        CmsPage one = optional.get();
        one.setTemplateId(cmsPage.getTemplateId());
        //更新所属站点
        one.setSiteId(cmsPage.getSiteId());
        //更新页面别名
        one.setPageAliase(cmsPage.getPageAliase());
        //更新页面名称
        one.setPageName(cmsPage.getPageName());
        //更新访问路径
        one.setPageWebPath(cmsPage.getPageWebPath());
        //更新物理路径
        one.setPagePhysicalPath(cmsPage.getPagePhysicalPath());
        one.setDataUrl(cmsPage.getDataUrl());
        one.setPageId(id);
        cmsPageRepository.save(one);
        return new CmsPageResult(CommonCode.SUCCESS, one);
    }

    //删除页面信息
    public ResponseResult deleteCmsPageById(String id) {
        if (StringUtils.isEmpty(id)) {
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Optional<CmsPage> optional = cmsPageRepository.findById(id);
        if (optional != null) {
            cmsPageRepository.deleteById(id);
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }



}